<template>
    <div>
        <el-form :model="ruleForm" status-icon ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
            <el-form-item label="手机号" prop="phone">
                <el-input type="number" v-model="ruleForm.phone" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import axios from 'axios'
  export default {
    data() {
      return {
        ruleForm: {
          phone: '',
          password: ''
        },
        rules: {
          phone: [
            { required: true, message: '请输入电话号码', trigger: 'blur' },
            { min:11, max: 11, message: '请输入正确格式的手机号码', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'change' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              this.$axios.post('/api/login', {
                  phone: this.ruleForm.phone,
                  password: this.$md5(this.ruleForm.password),
              }).then(res => {
                  if (res.data.code === 1) {
                      // 注册成功跳转到默认页面
                      this.$router.push('/')
                  } else {
                      alert('登录失败，请重试')
                  }
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>